package com.luhuiguo.cms.server.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.entity.Attachment;
import com.luhuiguo.cms.server.service.AttachmentService;
import com.luhuiguo.cms.server.util.MapperUtils;
import com.luhuiguo.cms.shared.model.AttachmentModel;
import com.luhuiguo.cms.shared.rpc.UpdateAttachment;
import com.luhuiguo.cms.shared.rpc.UpdateAttachmentResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class UpdateAttachmentHandler implements ActionHandler<UpdateAttachment, UpdateAttachmentResult> {


    @Autowired
    private AttachmentService attachmentService;

    @Override
    @PreAuthorize("isAuthenticated()")
    public UpdateAttachmentResult execute(UpdateAttachment action, ExecutionContext context) throws ActionException {
        Attachment a = MapperUtils.map(action.get(), Attachment.class);
        attachmentService.updateAttachment(a);
        return new UpdateAttachmentResult(action.get(), MapperUtils.map(a, AttachmentModel.class));
    }

    @Override
    public Class<UpdateAttachment> getActionType() {
        return UpdateAttachment.class;
    }

    @Override
    public void rollback(UpdateAttachment action, UpdateAttachmentResult result, ExecutionContext context)
            throws ActionException {

    }

}
